<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>伙伴讲堂</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../css/common.css" media="all">
	</head>

	<body>

		<div class="layui-fluid">
			<div class="layui-card">
				<div class="layui-card-header table-operate-btn">
					<!-- <span class="marign_right10">全部代理商</span> -->
					<button class="layui-btn layui-btn-normal layui-btn-sm" data-type='alladd'>添加</button>
				</div>
				<div class="layui-tab layui-tab-brief" lay-filter="changetab">
					<ul class="layui-tab-title" id="view_ul"></ul>
				</div>
				<div class="layui-card-body">
					<table id="test-table-operate" lay-filter="test-table-operate"></table>

					<script type="text/html" id="buttonTpl">
						{{# if(d.check == true){ }}
						<button class="layui-btn layui-btn-xs">已审核</button> {{# } else { }}
						<button class="layui-btn layui-btn-primary layui-btn-xs">未审核</button> {{# } }}
					</script>
					<script type="text/html" id="test-table-operate-barDemo">
						<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
						<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
					</script>
				</div>
			</div>
		</div>

		<script type="text/html" id="demo_ul">
			{{# layui.each(d, function(index, item){ }}
			<li class=" {{ index == 0 ? 'layui-this' : '' }}" lay-id='{{ item.id }}'>{{ item.name }}</li>
			{{# }); }}
		</script>

		<script src="../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../js/common.js" type="text/javascript" charset="utf-8"></script>
		<script>
			layui.config({
				base: '../../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'table'], function() {
				var $ = layui.$,
					form = layui.form,
					laytpl = layui.laytpl,
					element = layui.element,
					table = layui.table;

				var _data = {},
					catid = ''
				maplist = JSON.parse(localStorage.getItem('lsmap'))
				initnewscat();
				// initdata();

				//用户管理
				function initdata() {
					table.render({
						elem: '#test-table-operate',
						url: server.querynewslist,
						height: 'full-150',
						cols: [
							[{
									type: 'checkbox',
									fixed: 'left'
								},
								{
									width: 150,
									align: 'center',
									toolbar: '#test-table-operate-barDemo',
									title: '操作',
									fixed: 'left'
								}, {
									field: 'status',
									title: '状态',
									templet: function(d) {
										var html = ''
										for(var i = 0; i < maplist.on_off.length; i++) {
											if(maplist.on_off[i].value == String(d.status) && maplist.on_off[i].notes) {
												html = '<input type="checkbox" name="status" value=' + d.id +
													' lay-skin="switch" lay-text="启用|停用" lay-filter="status" checked=' + maplist.on_off[i].notes + '>'
											} else {
												html = '<input type="checkbox" name="status" value=' + d.id +
													' lay-skin="switch" lay-text="启用|停用" lay-filter="status">'
											}
										}
										return html;
									}
								}, 
								//{
								//	field: 'catname',
								//	title: '分类名称'
								//},
								{
									field: 'title',
									title: '标题'
								}, {
									field: 'thumbnail',
									title: '缩略图',
									templet: function(d) {
										return d.thumbnail ?
											'<a class="text_blue_color" lay-event="seeimg" href="javascript:;">查看缩略图</a>' : ''
									}
								}, {
									field: 'resource_url',
									title: '教程文件',
									templet: function(d) {
										if(d.resource_url){
											if(d.resource_url.indexOf('.ppt') > -1 || d.resource_url.indexOf('.doc') > -1 || d.resource_url.indexOf('.xl') > -1 || d.resource_url.indexOf('.pdf') > -1 || d.resource_url.indexOf('.txt') > -1) {
												return d.resource_url ?
													'<a class="text_blue_color" href=' + d.resource_url + '>下载/查看</a>' : ''
											} else {
												return d.resource_url ?
													'<a class="text_blue_color" lay-event="seeurl" href="javascript:;">下载/查看</a>' : ''
											}
										}else{
											return  ''
										}
									}
								}, {
									field: 'author',
									title: '作者',
								}, {
									field: 'phone',
									title: '浏览量',
								}
							]
						],
						page: true,
						where: _data,
						limit: limit,
						limits: limits,
						request: {
							pageName: 'page',
							limitName: 'size',
						},
						parseData: function(res) { //将原始数据解析成 table 组件所规定的数据
							return {
								"code": res.code, //解析接口状态
								"msg": res.msg, //解析提示文本
								"count": res.news_list.pager.total, //解析数据长度
								"data": res.news_list.data //解析数据列表
							};
						}
					});
				}

				element.on('tab(changetab)', function(elem) {
					catid = this.getAttribute('lay-id');
					_data.catid = catid;
					initdata();
				});

				//监听搜索
				form.on('submit(search)', function(data) {
					_data = data.field;
					_data.catid = catid
					initdata();
					return false;
				});

				$("button[type=reset]").on("click", function() {
					_data = {}
					_data.catid = catid
					initdata();
					table.reload('test-table-operate');
					document.getElementById("form").reset();
				});

				//监听工具条
				table.on('tool(test-table-operate)', function(obj) {
					var data = obj.data
					if(obj.event === 'seeimg') {
						var img_data = {
							"data": [{
								"src": data.resource_url
							}]
						}
						layer.photos({
							photos: img_data,
							anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机
						});
					} else if(obj.event === 'seeurl') {
						parent.window.open(data.resource_url)
					} else if(obj.event === 'edit') {
						layer.open({
							type: 2,
							title: '编辑',
							shadeClose: false,
							shade: 0.5,
							maxmin: true, //开启最大化最小化按钮
							area: ['60%', '90%'],
							content: '../model/class_edit_add.html?id=' + data.id,
							end: function() {}
						});
					} else if(obj.event === 'del') {
						layer.confirm('确定删除数据？', function() {
							loading();
							submitdata(server.delnews, {
								'id': data.id
							})
						}, function() {
							layer.msg('取消操作');
						});
					}
				});

				form.on('switch(status)', function(obj) {
					var id = this.value;
					layer.confirm('确定要修改状态？', function(index) {
						var data = {
							'id': id
						}
						if(obj.elem.checked) {
							data.status = 1
							submitdata(server.updatenews, data)
						} else {
							data.status = 0
							submitdata(server.updatenews, data)
						}
					}, function() {
						initdata();
						layer.msg('取消操作');
					});

				});
				/* 修改数据*/
				function submitdata(_url, _data) {
					$.post(_url, _data, function(res, status) {
						if(status == 'success') {
							if(res.code == 0) {
								layer.alert('操作成功', function() {
									layer.closeAll();
									initdata();
								});
							} else {
								layer.msg(res.msg);
								layer.closeAll('loading');
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}

				function initnewscat() {
					$.get(server.querynews_cat, {}, function(res, status) {
						if(status == 'success') {
							if(res.code == 0) {
								var data = res.news_cat.data;

								_data.catid = data[0].id
								initdata();

								var getTpl = demo_ul.innerHTML,
									view = document.getElementById('view_ul');
								laytpl(getTpl).render(data, function(html) {
									view.innerHTML = html;
								});
								element.init();
							} else {
								layer.msg(res.msg);
							}
							closeloading();
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}

				//事件
				var active = {
					alladd: function() {
						layer.open({
							type: 2,
							title: '添加',
							content: '../model/class_edit_add.html',
							area: ['900px', '80%'],
							yes: function(index, layero) {}
						});
					},
					batchdel: function() {
						var checkStatus = table.checkStatus('LAY-user-back-role'),
							checkData = checkStatus.data; //得到选中的数据

						if(checkData.length === 0) {
							return layer.msg('请选择数据');
						}

						layer.confirm('确定删除吗？', function(index) {

							table.reload('LAY-user-back-role');
							layer.msg('已删除');
						});
					}
				}
				$('.table-operate-btn .layui-btn').on('click', function() {
					var type = $(this).data('type');
					active[type] ? active[type].call(this) : '';
				});
			});
		</script>
	</body>

</html>